home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / c / host / RCS / Host_Next.c,v < prev    next >
Encoding:
Text File  |  1992-06-05  |  6.8 KB  |  347 lines

  1. head     1.7;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    jhh:1.7; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.7
  10. date     90.01.02.17.10.46;  author ouster;  state Exp;
  11. branches ;
  12. next     1.6;
  13.  
  14. 1.6
  15. date     89.08.23.18.34.47;  author douglis;  state Exp;
  16. branches ;
  17. next     1.5;
  18.  
  19. 1.5
  20. date     89.06.16.19.30.20;  author mendel;  state Exp;
  21. branches ;
  22. next     1.4;
  23.  
  24. 1.4
  25. date     89.06.08.09.27.30;  author mendel;  state Exp;
  26. branches ;
  27. next     1.3;
  28.  
  29. 1.3
  30. date     88.07.25.13.39.33;  author ouster;  state Exp;
  31. branches ;
  32. next     1.2;
  33.  
  34. 1.2
  35. date     88.07.18.10.53.08;  author mendel;  state Exp;
  36. branches ;
  37. next     1.1;
  38.  
  39. 1.1
  40. date     88.06.30.11.06.46;  author ouster;  state Exp;
  41. branches ;
  42. next     ;
  43.  
  44.  
  45. desc
  46. @@
  47.  
  48.  
  49. 1.7
  50. log
  51. @Convert inet addresses to host byte order.
  52. @
  53. text
  54. @/* 
  55.  * Host_Next.c --
  56.  *
  57.  *    Source code for the Host_Next library procedure.
  58.  *
  59.  * Copyright 1988 Regents of the University of California
  60.  * Permission to use, copy, modify, and distribute this
  61.  * software and its documentation for any purpose and without
  62.  * fee is hereby granted, provided that the above copyright
  63.  * notice appear in all copies.  The University of California
  64.  * makes no representations about the suitability of this
  65.  * software for any purpose.  It is provided "as is" without
  66.  * express or implied warranty.
  67.  */
  68.  
  69. #ifndef lint
  70. static char rcsid[] = "$Header: /sprite/src/lib/c/host/RCS/Host_Next.c,v 1.6 89/08/23 18:34:47 douglis Exp $ SPRITE (Berkeley)";
  71. #endif not lint
  72.  
  73. #include <stdio.h>
  74. #include <sprite.h>
  75. #include <ctype.h>
  76. #include <host.h>
  77. #include <hostInt.h>
  78. #include <stdlib.h>
  79. #include <string.h>
  80. #include <arpa/inet.h>
  81. #include <netinet/in.h>
  82.  
  83. /*
  84.  *-----------------------------------------------------------------------
  85.  *
  86.  * Host_Next --
  87.  *
  88.  *    Read the next line from the host file and break it into the
  89.  *    appropriate fields of the structure.
  90.  *
  91.  * Results:
  92.  *    The return value is a pointer to a Host_Entry structure
  93.  *    containing the information from the next line of the file.
  94.  *    This is a statically-allocated structure, which will only
  95.  *    retain its value up to the next call to this procedure.
  96.  *    If the end of the file is reached, or an error occurs, NULL
  97.  *    is returned.
  98.  *
  99.  * Side Effects:
  100.  *    The position in the file advances.
  101.  *
  102.  *-----------------------------------------------------------------------
  103.  */
  104. Host_Entry *
  105. Host_Next()
  106. {
  107. #define BUFFER_SIZE 512
  108. #define MAX_NAMES 20
  109.     static Host_Entry    entry;
  110.     static char          inputBuf[BUFFER_SIZE];
  111.     static char *    fields[MAX_NAMES+2];
  112.     register char *    p;
  113.     int            numFields;
  114.     int              c;
  115.  
  116.     if (hostFile == (FILE *) NULL) {
  117.     return ((Host_Entry *) NULL);
  118.     } else {
  119.     /*
  120.      * First skip any comment lines or blank lines.
  121.      */
  122.  
  123.     while (1) {
  124.         c = getc(hostFile);
  125.         if (c != '#' && c != '\n') {
  126.         break;
  127.         }
  128.         while ((c != '\n') && (c != EOF)) {
  129.         c = getc(hostFile);
  130.         } 
  131.     }
  132.     ungetc(c, hostFile);
  133.  
  134.     /*
  135.      * <spriteID>
  136.      */
  137.  
  138.     if (fscanf(hostFile, "%d", &entry.id) != 1) {
  139.         return ((Host_Entry *) NULL);
  140.     }
  141.  
  142.     /*
  143.      * <netType> and <netAddr>
  144.      */
  145.  
  146.     if (fscanf(hostFile, "%100s", inputBuf) != 1) {
  147.         return (Host_Entry *) NULL;
  148.     }
  149.     if (strcmp(inputBuf, "ether") == 0) {
  150.         /*
  151.          * A HOST_ETHER route address is the ethernet address of the
  152.          * machine.
  153.          */
  154.         int byte[6];
  155.         entry.netType = HOST_ETHER;
  156.         if (fscanf(hostFile, "%2x:%2x:%2x:%2x:%2x:%2x",
  157.             &byte[0], &byte[1], &byte[2], &byte[3], &byte[4], &byte[5])
  158.             != 6) {
  159.         return (Host_Entry *) NULL;
  160.         }
  161.         entry.netAddr.etherAddr[0] = byte[0];
  162.         entry.netAddr.etherAddr[1] = byte[1];
  163.         entry.netAddr.etherAddr[2] = byte[2];
  164.         entry.netAddr.etherAddr[3] = byte[3];
  165.         entry.netAddr.etherAddr[4] = byte[4];
  166.         entry.netAddr.etherAddr[5] = byte[5];
  167.     } else if (strcmp(inputBuf, "inet") == 0) {
  168.         /*
  169.          * A HOST_INET route address is the ethernet address of the
  170.          * first gateway machine. The ip address is taken from the
  171.          * field internetAddr.
  172.          */
  173.         int byte[6];
  174.         entry.netType = HOST_INET;
  175.         if (fscanf(hostFile, "%2x:%2x:%2x:%2x:%2x:%2x",
  176.             &byte[0], &byte[1], &byte[2], &byte[3], &byte[4], &byte[5])
  177.             != 6) {
  178.         return (Host_Entry *) NULL;
  179.         }
  180.         entry.netAddr.etherAddr[0] = byte[0];
  181.         entry.netAddr.etherAddr[1] = byte[1];
  182.         entry.netAddr.etherAddr[2] = byte[2];
  183.         entry.netAddr.etherAddr[3] = byte[3];
  184.         entry.netAddr.etherAddr[4] = byte[4];
  185.         entry.netAddr.etherAddr[5] = byte[5];
  186.  
  187.     } else {
  188.         return (Host_Entry *) NULL;
  189.     }
  190.  
  191.     /*
  192.      * <internetAddr>
  193.      */
  194.  
  195.     if (fscanf(hostFile, "%100s", inputBuf) != 1) {
  196.         return (Host_Entry *) NULL;
  197.     }
  198.     entry.inetAddr.s_addr = ntohl(inet_addr(inputBuf));
  199.  
  200.     /*
  201.      * <machType>:  first parse the remainder of the line up into
  202.      * fields.
  203.      */
  204.  
  205.     do {
  206.         c = getc(hostFile);
  207.     } while (isspace(c));
  208.     ungetc(c, hostFile);
  209.     if (fgets(inputBuf, BUFFER_SIZE, hostFile) == NULL) {
  210.         return (Host_Entry *) NULL;
  211.     }
  212.  
  213.     /*
  214.      * If the line didn't all fit in the buffer, throw away the
  215.      * remainder.
  216.      */
  217.  
  218.     for (p = inputBuf; *p !=0; p++) {
  219.         /* Null loop body */
  220.     }
  221.     if (p[-1] != '\n') {
  222.         do {
  223.         c = getc(hostFile);
  224.         } while ((c != '\n') && (c != EOF));
  225.     }
  226.  
  227.     for (p = inputBuf, numFields = 0; *p != 0; numFields++) {
  228.         fields[numFields] = p;
  229.         while (!isspace(*p)) {
  230.         p++;
  231.         }
  232.         *p = 0;
  233.         p++;
  234.         while (isspace(*p)) {
  235.         p++;
  236.         }
  237.         if (numFields == MAX_NAMES+1) {
  238.         break;
  239.         }
  240.     }
  241.     if (numFields < 2) {
  242.         return (Host_Entry *) NULL;
  243.     }
  244.     entry.machType = fields[0];
  245.  
  246.     /*
  247.      * <name> and <aliases>
  248.      */
  249.  
  250.     entry.name = fields[1];
  251.     entry.aliases = &fields[2];
  252.     fields[numFields] = (char *) NULL;
  253.     }
  254.     return &entry;
  255. }
  256. @
  257.  
  258.  
  259. 1.6
  260. log
  261. @allow blank lines in file
  262. @
  263. text
  264. @d17 1
  265. a17 1
  266. static char rcsid[] = "$Header: /sprite/src/lib/c/host/RCS/Host_Next.c,v 1.5 89/06/16 19:30:20 mendel Exp Locker: douglis $ SPRITE (Berkeley)";
  267. d28 1
  268. d145 1
  269. a145 1
  270.     entry.inetAddr.s_addr = inet_addr(inputBuf);
  271. @
  272.  
  273.  
  274. 1.5
  275. log
  276. @Fixed type.
  277. @
  278. text
  279. @d17 1
  280. a17 1
  281. static char rcsid[] = "$Header: /sprite/src/lib/c/host/RCS/Host_Next.c,v 1.4 89/06/08 09:27:30 mendel Exp $ SPRITE (Berkeley)";
  282. d66 1
  283. a66 1
  284.      * First skip any comment lines
  285. d71 1
  286. a71 1
  287.         if (c != '#') {
  288. d74 1
  289. a74 1
  290.         do {
  291. d76 1
  292. a76 1
  293.         } while ((c != '\n') && (c != EOF));
  294. @
  295.  
  296.  
  297. 1.4
  298. log
  299. @Added "inet" routes to the hostfile.
  300. @
  301. text
  302. @d17 1
  303. a17 1
  304. static char rcsid[] = "$Header: Host_Next.c,v 1.3 88/07/25 13:39:33 ouster Exp $ SPRITE (Berkeley)";
  305. d131 1
  306. @
  307.  
  308.  
  309. 1.3
  310. log
  311. @Lint.
  312. @
  313. text
  314. @d17 1
  315. a17 1
  316. static char rcsid[] = "$Header: Host_Next.c,v 1.2 88/07/18 10:53:08 mendel Exp $ SPRITE (Berkeley)";
  317. d96 4
  318. d113 19
  319. @
  320.  
  321.  
  322. 1.2
  323. log
  324. @Added include for <sprite.h> so module will compile. 
  325. @
  326. text
  327. @d17 1
  328. a17 1
  329. static char rcsid[] = "$Header: Host_Next.c,v 1.1 88/06/30 11:06:46 ouster Exp $ SPRITE (Berkeley)";
  330. d60 1
  331. a60 2
  332.     char              c;
  333.     Boolean        gotNewline;
  334. @
  335.  
  336.  
  337. 1.1
  338. log
  339. @Initial revision
  340. @
  341. text
  342. @d17 1
  343. a17 1
  344. static char rcsid[] = "$Header: proto.c,v 1.2 88/03/11 08:39:08 ouster Exp $ SPRITE (Berkeley)";
  345. d21 1
  346. @
  347.